package com.ruoyi.classai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.dto.ModuleLessonPreparationModificationDto;
import com.ruoyi.classai.domain.dto.PublicIdQueryInterfaceDto;
import com.ruoyi.classai.service.IEwcModuleResourceService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.ruoyi.classai.mapper.EwcLessonPreparationRelationMapper;
import com.ruoyi.classai.domain.EwcLessonPreparationRelation;
import com.ruoyi.classai.service.IEwcLessonPreparationRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 备课关联Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Service
public class EwcLessonPreparationRelationServiceImpl extends ServiceImpl<EwcLessonPreparationRelationMapper, EwcLessonPreparationRelation> implements IEwcLessonPreparationRelationService {

    @Autowired
    private IEwcModuleResourceService moduleResourceService;
    @Override
    public AjaxResult saveOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        ewcLessonPreparationRelation.setCourseId(publicIdQueryInterfaceDto.getCourseId());
        ewcLessonPreparationRelation.setClassId(publicIdQueryInterfaceDto.getClassId());
        ewcLessonPreparationRelation.setId(publicIdQueryInterfaceDto.getChapterUniqueId());
        ewcLessonPreparationRelation.setModuleState("1");
        ewcLessonPreparationRelation.setModuleCode(publicIdQueryInterfaceDto.getModuleCode());
        ewcLessonPreparationRelation.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        ewcLessonPreparationRelation.setChapterUniqueId(publicIdQueryInterfaceDto.getChapterUniqueId());
//            QueryWrapper<EwcModuleResource> queryWrapper = new QueryWrapper<>();
//            // 设置查询条件，根据 field1 进行查询
//            queryWrapper.eq("module_code", publicIdQueryInterfaceDto.getModuleCode());
//            // 只查询 field2 字段
//            queryWrapper.select("module_resource_unique_id");
//            EwcModuleResource uniqueId = moduleResourceService.getOne(queryWrapper);
//            ewcLessonPreparationRelation.setModuleResourceUniqueId(uniqueId.getModuleResourceUniqueId());

//        ewcLessonPreparationRelation.setType(publicIdQueryInterfaceDto.getChapterUniqueId());

        try {
                // 调用 MyBatis-Plus 提供的插入方法
                boolean result = save(ewcLessonPreparationRelation);
                if (result) {
                    return AjaxResult.success("数据插入成功");
                } else {
                    return AjaxResult.error("数据插入失败");
                }
            } catch (DuplicateKeyException e) {
                return AjaxResult.error("数据已存在");
            }
        }

    @Override
    public boolean deleteOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {

        // 创建 QueryWrapper 对象
        QueryWrapper<EwcLessonPreparationRelation> queryWrapper = new QueryWrapper<>();
        // 根据实体属性设置删除条件
        queryWrapper.eq("id", publicIdQueryInterfaceDto.getChapterUniqueId());
        queryWrapper.eq("module_code", publicIdQueryInterfaceDto.getModuleCode());
        queryWrapper.eq("class_id", publicIdQueryInterfaceDto.getClassId());
        queryWrapper.eq("course_id", publicIdQueryInterfaceDto.getCourseId());
        queryWrapper.eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
        // 执行删除操作
        int rows = this.baseMapper.delete(queryWrapper);
        return rows > 0;
    }

    @Override
    public boolean updateState(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcLessonPreparationRelation> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("course_id", publicIdQueryInterfaceDto.getCourseId())
                .eq("class_id", publicIdQueryInterfaceDto.getClassId())
                .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId())
                .eq("id", publicIdQueryInterfaceDto.getChapterUniqueId());
        // 设置要更新的字段及其值
        EwcLessonPreparationRelation entity = new EwcLessonPreparationRelation();
        entity.setModuleState("2");
        // 执行更新操作
        return this.update(entity, updateWrapper);
    }

    @Override
    public void updateStateWithModuleCode(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcLessonPreparationRelation> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("course_id", moduleLessonPreparationModificationDto.getCourseId())
                .eq("class_id", moduleLessonPreparationModificationDto.getClassId())
                .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId())
                .eq("id", moduleLessonPreparationModificationDto.getChapterUniqueId())
                .eq("module_code", moduleLessonPreparationModificationDto.getModuleCode());
        // 设置要更新的字段及其值
        EwcLessonPreparationRelation entity = new EwcLessonPreparationRelation();
        entity.setModuleState("2");
        // 执行更新操作
        this.update(entity, updateWrapper);
    }


}

